
#ifndef MBSERVER_H
#define MBSERVER_H

#include <QObject>
#include <QMutex>
#include <QVector>
#include <QModbusTcpServer>
#include <QModbusResponse>
#include <QModbusPdu>
#include <QTimer>
#include "datatype.h"

// 自定义ModbusTcpServer类
class CustomModbusServer : public QModbusTcpServer
{
    Q_OBJECT
public:
    explicit CustomModbusServer(QObject* parent = nullptr);

    bool writeData(const QModbusDataUnit& unit);
    bool readData(QModbusDataUnit* newData) const;
    bool writeDataUnit(const QModbusDataUnit& unit);
    bool readDataUnit(QModbusDataUnit* newData) const;
    
    // 添加访问寄存器的方法
    QVector<quint16>& getHoldingRegisters() { return m_holdingRegisters; }
    const QVector<quint16>& getHoldingRegisters() const { return m_holdingRegisters; }

protected:
    QModbusResponse processRequest(const QModbusPdu &request) override;
    bool writeRegister(quint16 address, quint16 value);
    bool readRegister(quint16 address, quint16* value) const;

private:
    mutable QMutex m_mutex;
    QVector<quint16> m_holdingRegisters;
    QVector<quint16> m_inputRegisters;
};

// ModbusServer类
class ModbusServer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(bool running READ isRunning NOTIFY runningChanged)
    Q_PROPERTY(QString status READ status NOTIFY statusChanged)
    Q_PROPERTY(int deviceId READ deviceId NOTIFY deviceDataChanged)
    Q_PROPERTY(QString deviceName READ deviceName NOTIFY deviceDataChanged)
    Q_PROPERTY(float deviceValue READ deviceValue NOTIFY deviceDataChanged)
    Q_PROPERTY(bool deviceStatus READ deviceStatus NOTIFY deviceDataChanged)
    Q_PROPERTY(qint32 deviceTimestamp READ deviceTimestamp NOTIFY deviceDataChanged)
    Q_PROPERTY(float temperature READ temperature NOTIFY deviceDataChanged)
    Q_PROPERTY(float humidity READ humidity NOTIFY deviceDataChanged)
    Q_PROPERTY(int pressure READ pressure NOTIFY deviceDataChanged)
    Q_PROPERTY(int alarmStatus READ alarmStatus NOTIFY deviceDataChanged)
    Q_PROPERTY(QString description READ description NOTIFY deviceDataChanged)

public:
    explicit ModbusServer(QObject *parent = nullptr);
    ~ModbusServer();

    Q_INVOKABLE bool startServer(int port);
    Q_INVOKABLE void stopServer();
    Q_INVOKABLE void updateData(const DeviceData& data);

    bool isRunning() const;
    QString status() const;

    // 数据访问方法
    int deviceId() const { return m_deviceData.id; }
    QString deviceName() const { return m_deviceData.name; }
    float deviceValue() const { return m_deviceData.value; }
    bool deviceStatus() const { return m_deviceData.status; }
    qint32 deviceTimestamp() const { return m_deviceData.timestamp; }
    float temperature() const { return m_deviceData.temperature; }
    float humidity() const { return m_deviceData.humidity; }
    int pressure() const { return m_deviceData.pressure; }
    int alarmStatus() const { return m_deviceData.alarmStatus; }
    QString description() const { return m_deviceData.description; }

signals:
    void runningChanged();
    void statusChanged();
    void deviceDataChanged();
    void errorOccurred(const QString& error);

private slots:
    void handleDeviceError(QModbusDevice::Error error);
    void updateSimulatedData();
    void handleDataWritten(QModbusDataUnit::RegisterType type, int address, int size);

private:
    void setupInitialData();
    void updateModbusData();
    void setStatus(const QString& status);
    void updateDeviceDataFromRegisters();

    CustomModbusServer* modbusServer;
    QString m_status;
    DeviceData m_deviceData;
    QTimer* simulationTimer;
};

#endif // MBSERVER_H
